package main
import (
	"fmt"
	"errors"
)

//用一个结构体来模拟一个栈
type Stack struct{
	MaxTop int  //栈顶最大值，能存的最大数据量
	Top int 	//表示栈顶
	arr [5]int
}


func main(){
	stack := &Stack{
		MaxTop : 5,
		Top : -1,
	}
	stack.Push(11)
	stack.Push(12)
	stack.Push(13)
	stack.Push(14)
	stack.Push(15)
	err := stack.Push(16)
	if err != nil{
		fmt.Println("Push Error :",err)
	}
	stack.Show()
	val,err := stack.Pop()
	if err != nil{
		fmt.Println("Pop Error :",err)
	}
	fmt.Println("取出了栈顶的值：",val)
	stack.Show()
}

//入栈
func (this *Stack)Push(val int)(err error){
	if this.Top == this.MaxTop - 1{ //栈满
		return errors.New("栈满了~")
	}

	this.Top++
	this.arr[this.Top] = val
	return 
}

//出栈
func (this *Stack)Pop()(val int,err error){
	//先判断是否为空
	if this.Top == -1{
		return -1,errors.New("栈是空的咋弹出啊~")
	}
	val = this.arr[this.Top]
	this.Top--
	return
}

//遍历
func (this *Stack)Show(){
	//先判断是否为空
	if this.Top == -1{
		fmt.Println("栈是空的咋遍历啊~")
	}
	for curTop := this.Top;curTop >=0;curTop--{
		fmt.Printf("栈[%d]:%v\n",curTop,this.arr[curTop])
	}
}